package com.xianggu.trafficLimiter.demo;

import com.xianggu.trafficLimiter.TrafficLimiter;
import com.xianggu.trafficLimiter.impl.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author chengjy
 * @date 2025/3/25
 */
class TrafficDemo {
    public static void main(String[] args) throws InterruptedException {
        dateWindowBucketLimiter();
//        redisBucketLimiter();
//        counterBucketLimiter();
//        tokenBucketLimiter();
//        leakyBucketLimiter();
    }

    private static void leakyBucketLimiter() {
        TrafficLimiter trafficLimiter = new LeakyBucketLimiter(100, 100);
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

        for (int i = 0; i < 150; i++) {
            // 每秒时间
//            Thread.sleep(50);
            if (trafficLimiter.limit()) {
                System.out.println(format.format(new Date())+ " 限流了");
            }else{
                System.out.println(format.format(new Date())+" 正常访问");
            }
        }
    }

    public static void tokenBucketLimiter() throws InterruptedException {
    TrafficLimiter trafficLimiter = new TokenBucketLimiter();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

        long numberOfSuccesses = 0;
        long numberOfFailures = 0;
        for (int i = 0; i < 300; i++) {
            // 每秒时间
            Thread.sleep(5);
            if (trafficLimiter.limit()) {
                numberOfFailures++;
                System.out.println(format.format(new Date())+ " 限流了");
            }else{
                numberOfSuccesses++;
                System.out.println(format.format(new Date())+" 正常访问");
            }
        }
        System.out.println("成功次数：" + numberOfSuccesses);
        System.out.println("失败次数：" + numberOfFailures);
    }
    public static void counterBucketLimiter() throws InterruptedException {
    TrafficLimiter trafficLimiter = new CounterBucketLimiter();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

        long numberOfSuccesses = 0;
        long numberOfFailures = 0;
        for (int i = 0; i < 300; i++) {
            // 每秒时间
            Thread.sleep(5);
            if (trafficLimiter.limit()) {
                numberOfFailures++;
//                System.out.println(format.format(new Date())+ " 限流了");
            }else{
                numberOfSuccesses++;
                System.out.println(format.format(new Date())+" 正常访问");
            }
        }
        System.out.println("成功次数：" + numberOfSuccesses);
        System.out.println("失败次数：" + numberOfFailures);
    }
    public static void redisBucketLimiter() throws InterruptedException {
    TrafficLimiter trafficLimiter = new RedisBucketLimiter();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

        long numberOfSuccesses = 0;
        long numberOfFailures = 0;
        for (int i = 0; i < 300; i++) {
            // 每秒时间
            Thread.sleep(5);
            if (trafficLimiter.limit()) {
                numberOfFailures++;
//                System.out.println(format.format(new Date())+ " 限流了");
            }else{
                numberOfSuccesses++;
                System.out.println(format.format(new Date())+" 正常访问");
            }
        }
        System.out.println("成功次数：" + numberOfSuccesses);
        System.out.println("失败次数：" + numberOfFailures);
    }
    public static void dateWindowBucketLimiter() throws InterruptedException {
    TrafficLimiter trafficLimiter = new DateWindowBucketLimiter();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

        long numberOfSuccesses = 0;
        long numberOfFailures = 0;
        for (int i = 0; i < 300; i++) {
            // 每秒时间
            Thread.sleep(5);
            if (trafficLimiter.limit()) {
                numberOfFailures++;
//                System.out.println(format.format(new Date())+ " 限流了");
            }else{
                numberOfSuccesses++;
                System.out.println(format.format(new Date())+" 正常访问");
            }
        }
        System.out.println("成功次数：" + numberOfSuccesses);
        System.out.println("失败次数：" + numberOfFailures);

        trafficLimiter.stopThread();
    }
}
